<template>
  <BaseForm
    labelWidth="100px"
    :fromIems="fromIems"
    :size="'medium'"
    ref="BaseForm"
    @submit="submit"
    @edit="edit"
  >
    <template #topbtn>
      <el-button icon="el-icon-setting"  type="primary" @click="confrim">发送调光命令</el-button>
    </template>
  </BaseForm>
</template>

<script>
import { Message } from "element-ui";
import { add, edit } from "@/api/role";
import { getDeviceInfo, sendTGcmd } from "@/api/device";
export default {
  data() {
    return {
      title: "",
      fromIems: [
        {
          valueType: "input",
          label: "地址码",
          dataIndex: "address",
          props:{
            placeholder:'请输入(1-512)',
            style:{
              width:'220px'
            }
          }
        },
        {
          valueType: "select",
          label: "通道模式",
          dataIndex: "channelMode",
          options: [
            // {
            //   label: "模式1(8CH)",
            //   value: 8,
            // },
            // {
            //   label: "模式2(16CH)",
            //   value: 16,
            // },
            // {
            //   label: "模式2(32CH)",
            //   value: 32,
            // },
          ],
        },
        {
          valueType: "select",
          label: "调光模式",
          dataIndex: "dimmingMode",
          options: [
            {
              label: "Stan",
              value: 0,
            },
            {
              label: "Off",
              value: 1,
            },
            {
              label: "Fast",
              value: 2,
            },
            {
              label: "Slow",
              value: 3,
            },
          ],
        },
        {
          valueType: "select",
          label: "调光曲线",
          dataIndex: "dimmingCurve",
          options: [
            {
              label: "L",
              value: 0,
            },
            {
              label: "E",
              value: 1,
            },
            {
              label: "S",
              value: 2,
            },
            {
              label: "RE",
              value: 3,
            },
          ],
        },
        {
          valueType: "select",
          label: "调光频率",
          dataIndex: "dimmingFreq",
          options: [
            {
              label: "4K",
              value: 0,
            },
            {
              label: "10K",
              value: 1,
            },
            {
              label: "20K",
              value: 2,
            },
            {
              label: "40K",
              value: 3,
            },
          ],
        },
        {
          valueType: "select",
          label: "识别开关",
          dataIndex: "distinguishSwitch",
          options: [
            {
              label: "关闭",
              value: 0,
            },
            {
              label: "打开",
              value: 1,
            },
          ],
        },
        {
          valueType: "select",
          label: "通道开关",
          dataIndex: "channelSwitch",
          options: [
            {
              label: "关闭",
              value: 0,
            },
            {
              label: "打开",
              value: 1,
            },
          ],
        },
      ],
      isEdit: false,
    };
  },
  methods: {
    open(res,cmds) {
      const info = res.content;
      if(cmds && JSON.parse(cmds).length){
        JSON.parse(cmds).forEach((el,index)=>{
          this.fromIems[1].options.push({
            value:index,
            label:el
          })
        })
      

      }else{
        this.fromIems[1].options = [
          {value:0,label:'模式一'},
          {value:1,label:'模式二'},
          {value:2,label:'模式三'},
        ]
      }

      if (info.dimChannelInfo && JSON.parse(info.dimChannelInfo).address) {
        this.$set(
          this.$refs.BaseForm.form,
          "address",
          JSON.parse(info.dimChannelInfo).address
        );
        this.$set(
          this.$refs.BaseForm.form,
          "channelMode",
          JSON.parse(info.dimChannelInfo).channelMode
        );
        this.$set(
          this.$refs.BaseForm.form,
          "dimmingMode",
          JSON.parse(info.dimChannelInfo).dimmingMode
        );
        this.$set(
          this.$refs.BaseForm.form,
          "dimmingCurve",
          JSON.parse(info.dimChannelInfo).dimmingCurve
        );
        this.$set(
          this.$refs.BaseForm.form,
          "dimmingFreq",
          JSON.parse(info.dimChannelInfo).dimmingFreq
        );
        this.$set(
          this.$refs.BaseForm.form,
          "distinguishSwitch",
          JSON.parse(info.dimChannelInfo).distinguishSwitch
        );
        this.$set(
          this.$refs.BaseForm.form,
          "channelSwitch",
          JSON.parse(info.dimChannelInfo).channelSwitch
        );
      }

      this.$nextTick(() => {
        if (res) {
          this.$set(this.$refs.BaseForm.form, "imei", res.content["imei"]);
        } else {
          this.$refs.BaseForm.form = {};
        }
      });
    },
    close() {
      this.$refs.baseDialog.close();
      this.$refs.BaseForm.rest();
    },
    confrim() {
      if (this.isEdit) {
        this.$refs.BaseForm.edit();
      } else {
        this.$refs.BaseForm.submit();
      }
    },
    async submit(data) {
      const res = await sendTGcmd(data);
      if (!res.status) {
        Message.success("发送调光命令成功");
      }
    },
    async edit(data) {
      const res = await sendTGcmd({
        imei: data.imei,
        address: data.address,
        channelMode: data.channelMode,
        dimmingMode: data.dimmingMode,
        dimmingCurve: data.dimmingCurve,
        dimmingFreq: data.dimmingFreq,
        distinguishSwitch: data.distinguishSwitch,
        channelSwitch: data.channelSwitch,
      });
      if (!res.status) {
        Message.success("发送调光命令成功");
      }
    },
    handleSearch(){
      this.$emit('handleSearch')
    }
  },
};
</script>

<style lang="scss" scoped>

</style>
